package com.power.services.question;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.power.dao.question.TiQuestionDAO;
import com.power.model.question.TiQuestion;
import com.power.model.tiCase.TICase;

@Service("TiQuestionService")
public class TiQuestionServiceImpl implements TiQuestionService {

	@Autowired
	private TiQuestionDAO dao;

	public void create(TiQuestion question) {
		this.dao.save(question);
	}

	public TiQuestion read(Integer id) {
		return this.dao.get(id);
	}

    public void saveOrUpdate(TiQuestion question) {
    	dao.saveOrUpdate(question);
    }

    public void delete(String ids){
        String[] idAry = ids.split("_");
        for (int i = 0; i < idAry.length; i++) {
            Integer id = new Integer(idAry[i]);
            dao.deleteByKey(id);
        }
    }

	public List<TiQuestion> findListByMemberId(Integer questionid) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TiQuestion.class);
            criteria.add(Restrictions.like("createAuthor", questionid));
        return dao.findByCriteria(criteria);
	}

	
	@SuppressWarnings("unchecked")
    public Map<String, Object> findTiQuestion(Map<String, String> paramMap) {
        Map<String, Object> restulMap = new HashMap<String, Object>();
        int start = Integer.parseInt(paramMap.get("start"));
        int limit = Integer.parseInt(paramMap.get("limit"));
        String str = " select  ts   from TiQuestion ts where  1=1 ";
        if (!"-1".equals(paramMap.get("replyFlag"))) {
            str += " and ts.isReply =" + paramMap.get("replyFlag");
        }
        if (!"".equals(paramMap.get("title"))&&!"-1".equals(paramMap.get("title"))) {
            str += " and ts.title like '%" + paramMap.get("title") + "%'";
        }
        if (!"-1".equals(paramMap.get("startDate"))&&!"".equals(paramMap.get("startDate"))&&!"".equals(paramMap.get("endDate"))
                && !"-1".equals(paramMap.get("endDate"))) {
            String stareDate = paramMap.get("startDate");
            String endDate = paramMap.get("endDate");
            str += " and ( ts.createTime between '" + stareDate + " 00:00:00' and '"
                    + endDate + " 23:59:59' ) ";
        }
        List<TiQuestion> data = dao.find(str, start, limit);
        List<TiQuestion> countData = dao.find(str);
        int count = countData == null ? 0 : countData.size();
        restulMap.put("data", data);
        restulMap.put("count", count);
        return restulMap;
    }
	
	
	public void delete(TiQuestion arg0) {
		dao.delete(arg0);
	}

	public void update(TiQuestion arg0) {
		dao.update(arg0);
	}

}